<template>
  <div>
    <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
      <!--
        show-checkbox: 是否显示多选框
        check-strictly：是否父子选择关联
        default-expanded-keys：[默认选择的项对应的值]（值来自于node-key对应的字段）
        node-key：指定选中该项后的值
        this.$refs.tree.setCheckedKeys([需要选中的值]) 可以设置需要选中的项
        this.$refs.tree.getCheckedKeys() 可以获取已选择的项
       -->
      <el-tree
        v-if="bol"
        ref="tree"
        :data="treeList"
        :props="{ label: 'name' }"
        show-checkbox
        check-strictly
        :default-expanded-keys="defaultKeys"
        node-key="id"
      />
      <template #footer>
        <div>
          <el-button @click="closeEvent">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      bol: true,
      treeList: [],
      defaultKeys: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取树形数据
    async getData() {
      const res = await sysPermission()
      this.treeList = changeData(res.data, '0')
    },
    // 关闭弹框
    closeEvent() {
      this.$emit('update:isShow', false)
      // this.bol = false
      // this.$nextTick(() => {
      //   this.bol = true
      // })

      // 清空之前的选中
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取角色详情
    async getRoleInfo(row) {
      const res = await sysRoleById(row.id)
      this.id = row.id
      // this.defaultKeys = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 分配权限
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('权限分配成功')
      this.closeEvent()
    }
  }
}
</script>

<style lang="scss" scoped></style>
